<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/common.jsp"%>
<html>
<head>
    <title>Title</title>
    <SCRIPT>
      var setting = {
        data: {
          simpleData: {
            enable: true
          }
        },check: {
              enable: true,
              autoCheckTrigger: true
          },
          async: {
              //true表示开启异步加载模式
              enable: true,
              url: "<%=path%>/menu/getZtreeMenu"
          },
          //view : 修改,删除,添加
          view : {
              addHoverDom: function(treeId, treeNode){
                  var aObj = $("#" + treeNode.tId + "_a");
                  if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
                  var s = '<span id="btnGroup'+treeNode.tId+'">';
                  if ( treeNode.level == 0 ) {
                      s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" >添加</a>';
                  } else if ( treeNode.level == 1 ) {
                      s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" title="修改权限信息">修改</a>';
                      if (treeNode.children == undefined) {
                          s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')"  >删除</a>';
                      }
                      s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
                  } else if ( treeNode.level == 2 ) {
                      s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
                      s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
                  }
                  s += '</span>';
                  aObj.after(s);
              },
              removeHoverDom: function(treeId, treeNode){
                  $("#btnGroup"+treeNode.tId).remove();
              }
          }
      };

  
      $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting);
      });

      function addNode(nodeId){
          //点击增加按钮,弹出窗口,跳转增加页面
          BootstrapDialog.show({
              title: '增加',
              message: $('<div></div>').load("<%=path%>/menu/toAdd"),
              //弹出框的大小
              size: BootstrapDialog.SIZE_WIDE,
              buttons: [{
                  label: '取消',
                  action: function (dialog) {
                      dialog.close();
                  }
              }, {
                  label: '确定',
                  action: function (dialog) {

                      $.ajax({
                          url : "<%=path%>/menu/add",
                          type : "post",
                          dataType : "json",
                          data : $("#add_form").serialize() + "&pid=" + nodeId,
                          success : function (result){
                              if (result){
                                  toastr.success("增加成功");
                                  dialog.close();
                                  $.fn.zTree.init($("#treeDemo"), setting);
                              }

                          },
                          error : function (result){
                              toastr.error("增加失败");
                              console.log(result);
                          }
                      })
                  }
              }]
          });
      }


      function editNode(id){
          //点击修改按钮,弹出窗口,跳转修改页面
          BootstrapDialog.show({
              title: '修改',
              message: $('<div></div>').load("<%=path%>/menu/toUpdate/" + id),
              //弹出框的大小
              size: BootstrapDialog.SIZE_WIDE,
              buttons: [{
                  label: '取消',
                  action: function (dialog) {
                      dialog.close();
                  }
              }, {
                  label: '确定',
                  action: function (dialog) {
                      $.ajax({
                          url : "<%=path%>/menu/update",
                          type : "get",
                          dataType : "json",
                          data : $("#update_form").serialize(),
                          success : function (result){
                              if (result){
                                  toastr.success("修改成功");
                                  dialog.close();
                                  $.fn.zTree.init($("#treeDemo"), setting);
                              }

                          },
                          error : function (result){
                              toastr.error("修改失败");
                              console.log(result);
                          }
                      })
                  }
              }]
          });
      }

      function deleteNode(id){
          if (confirm("确认删除?")){
              $.ajax({
                  url : "<%=path%>/menu/deleteById/"+id,
                  type : "get",
                  dataType : "json",
                  success : function (result){
                      if (result){
                          toastr.success("删除成功");
                          $.fn.zTree.init($("#treeDemo"), setting);
                      }
                  },
                  error : function (result){
                      toastr.error("删除失败");
                      console.log(result);
                  }
              })
          }
      }
    </SCRIPT>
</head>
<body>
    <div class="zTreeDemoBackground left">
      <ul id="treeDemo" class="ztree"></ul>
    </div>
</body>
</html>
